<?php
class Gallery_MediaController extends Promotor_Controller_Action_Scaffold {
	
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_gallery_media',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),

		// specjalizuje template dla akcji
		'display' => array(
			'layout' => 'gallery_media_display'
		)
	);
	
	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'move' => 'update',
		'display' => 'show',
		'thumb' => 'show',
		'delete' => 'delete'
	);
	
	public $ajaxable = array(
		'display' => array('html')
	);
	
	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Gallery_Model_Media';
	protected $_formAddClass = 'Gallery_Form_MediaAdd';
	protected $_formEditClass = 'Gallery_Form_MediaEdit';
	protected $_formRemoveClass = 'Gallery_Form_MediaRemove';
	
	public function indexAction() {
		$this->_forward('list');
	}
	
	public function init() {
		$ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->initContext('html');
	}

	public function displayAction() {
		$model = new Gallery_Model_Media();
		$table = $model->getDbTable();
		
		$row = $model->findById($this->_getParam('id'));

		$this->view->row = $row;

		if ($model->getStatus() === Gallery_Model_Media::FAILURE) {
			$this->_helper->viewRenderer->render('display.404');
		}
	}
	
	public function listAction() {
		$model = new Gallery_Model_Media();
		$table = $model->getDbTable();

		$rq = $this->getRequest();
		if ($rq->isPost()) {
			// akcje z listy wyboru
			switch ($rq->getPost('action_type')) {
				case 'update':
					if (null !== $rq->getPost('editable')) {
						if ($this->_helper->acl->isAllowed('update')) {
							$data = $rq->getPost('editable');
							$model->editableUpdate($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
				case 'delete':
					if (null !== $rq->getPost('action_checked')) {
						if ($this->_helper->acl->isAllowed('delete')) {
							$data = $rq->getPost('action_checked');
							$model->editableDelete($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
			}

			// przenosiny zaznaczonych rekordów...
			if ($rq->getPost('move_action')) {
				if (null !== $rq->getPost('action_checked')) {
					if ($this->_helper->acl->isAllowed('update')) {
						$data = $rq->getPost('action_checked');
						$model->editableUpdateMove($data, $rq->getPost('move_album_id'));
					}
				}
			}
		}

		// setup data grid
		$config = $this->_helper->config('media.xml');
		$grid = KontorX_DataGrid::factory($model->selectListGrid(), $config->grid);
		$grid->setValues($this->_getAllParams());

	 	$this->_setupDataGridPaginator($grid);
		
		$this->view->grid = $grid;
	}
}